/*
 * @Author: Broin
 * @Date: 2022-01-12 16:13:25
 * @LastEditTime: 2022-01-13 10:12:51
 * @LastEditors: your name
 * @Description: 
 * @FilePath: /myCore/os/src/logging.rs
 * MADE BY BROIN
 */


use core::fmt;
use crate::console;
use log::{self, Level, LevelFilter, Log, Metadata, Record};

pub fn init() {
    static LOGGER: Logger = Logger;
    log::set_logger(&LOGGER).unwrap();
    println!("Start to init");
    log::set_max_level(match option_env!("LOG") {
        Some("error") => LevelFilter::Error,
        Some("warn") => LevelFilter::Warn,
        Some("info") => LevelFilter::Info,
        Some("debug") => LevelFilter::Debug,
        Some("trace") => LevelFilter::Trace,
        _ => LevelFilter::Off,
    });
}

struct Logger;
impl Log for Logger {
    fn enabled(&self, _metadata: &Metadata) -> bool {
        true
    }
    
    fn log(&self, record: &Record) {
        if !self.enabled(record.metadata()) {
            return
        }

        print!("\x1b[{}m", level_to_color_code(record.level()));
        println!("[{}] {}", record.level(), record.args());
        print!("\x1b[0m")
    }

    fn flush(&self) {}
}

fn level_to_color_code(level: Level) -> u8 {
    match level {
        Level::Error => 31, // Red
        Level::Warn => 93,  // BrightYellow
        Level::Info => 34,  // Blue
        Level::Debug => 32, // Green
        Level::Trace => 90, // BrightBlack
    }
}


//  use core::fmt;
//  use crate::console;
//  use log::{self, Level, LevelFilter, Log, Metadata, Record};

//  pub fn init() {
//      static LOGGER: SimpleLogger = SimpleLogger;
//      log::set_logger(&LOGGER).unwrap();
//      log::set_max_level(match option_env!("LOG") {
//          Some("error") => LeverFilter::Error,
//          Some("warn") => LeverFilter::Warn,
//          Some("info") => LevelFilter::Info,
//          Some("debug") => LevelFilter::Debug,
//          Some("trace") => LevelFilter::Trace,
//          _ => LevelFilter::Off,
//      });
//  }

//  struct Logger;
//  impl Log for Logger {
//      fn enabled(&self, _metadata: &Metadata) -> bool {
//          true
//      }

//      fn log(&self, record: &Record) {
//          if !self.enabled(record.metadata()) {
//              return
//          }

//          print!("\x1b[{}m", level_to_color_code(record.level()));
//          println!("[{}] {}", record.level(), record.args());
//          print!("\x1b[0m");
//      }

//      fn flush(&self) {}
//  }

//  fn level_to_color_code(level: Level) -> u8 {
//      match level {
//          Level::Error => 31,    //Red.
//          Level::Warn => 93,     //BrightBlue.
//          Level::Info => 34,     //Blue.
//          Level::Debug => 32,    //Green.
//          Level::Trace => 90,    //BrightBlack.
//      }
//  }

// use core::fmt;
// use crate::console;
// use log::{self, Level, LevelFilter, Log, Metadata, Record};

// pub fn init() {
//     static LOGGER: Logger = Logger;
//     log::set_logger(&LOGGER).unwrap();
//     log::set_max_level(match option_env!("LOG") {
//         Some("error") => LevelFilter::Error,
//         Some("warn") => LevelFilter::Warn,
//         Some("info") => LevelFilter::Info,
//         Some("debug") => LevelFilter::Debug,
//         Some("trace") => LevelFilter::Trace,
//         _ => LevelFilter::Off,
//     });
// }

// struct Logger;
// impl Log for Logger {
//     fn enabled(&self, _metadata: &Metadata) -> bool {
//         true
//     }

//     fn log(&self, record: &Record) {
//         if !self.enabled(record.metadata()) {
//             return
//         }

//         print!("\x1b[{}m", level_to_color_code(record.level()));
//         println!("[{}] {}", record.level(), record.args());
//         print!("\x1b[0m")
//     }

//     fn flush(&self) {}
// }

// fn level_to_color_code(level: Level) -> u8 {
//     match level {
//         Level::Error => 31, // Red
//         Level::Warn => 93,  // BrightYellow
//         Level::Info => 34,  // Blue
//         Level::Debug => 32, // Green
//         Level::Trace => 90, // BrightBlack
//     }
// }